<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>导航栏和内容框示例</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
        }

        /* 导航栏样式 */
        .navbar {
            display: flex;
            align-items: center;
            justify-content: space-between;
            background-color: #333;
            padding: 10px 20px;
            color: white;
        }
        .navbar .logo img {
            height: 40px;
        }
        .navbar .system-name {
            flex: 1;
            text-align: center;
            font-size: 24px;
            font-weight: bold;
        }
        .navbar .titles a {
            text-decoration: none;
            color: white;
            font-weight: bold;
        }
        .navbar .titles a:hover {
            text-decoration: underline;
        }

        /* 页面布局 */
        .main-container {
            display: flex;
            height: calc(100vh - 60px); /* 除去导航栏的高度 */
        }

        /* 左侧垂直菜单样式 */
        .sidebar {
            width: 200px;
            border-right: 1px solid #ccc;
            background-color: #f9f9f9;
            padding: 10px;
            box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
        }
        .menu-item {
            position: relative;
            cursor: pointer;
            padding: 10px;
            border-bottom: 1px solid #eee;
        }
        .menu-item:hover {
            background-color: #e0e0e0;
        }
        .menu-item:hover::after {
            content: attr(data-tooltip);
            position: absolute;
            left: 100%;
            top: 50%;
            transform: translateY(-50%);
            background-color: #333;
            color: white;
            padding: 5px 10px;
            border-radius: 4px;
            white-space: nowrap;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        }
        .submenu {
            display: none;
            background-color: #fff;
            padding-left: 20px;
            margin-top: 5px;
            border-left: 2px solid #ccc;
        }
        .submenu a {
            display: block;
            padding: 5px 0;
            color: #333;
            text-decoration: none;
        }
        .submenu a:hover {
            text-decoration: underline;
        }
        .menu-item.active .submenu {
            display: block;
        }

        /* 右侧内容框样式 */
        .content {
            flex: 1;
            padding: 20px;
            background-color: #fff;
            overflow-y: auto;
            box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
        }
        .content h1 {
            margin-top: 0;
        }

        /* 表格样式 */
        .hr-archive-table {
            width: 100%;
            border-collapse: collapse;
        }
        .hr-archive-table th, .hr-archive-table td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .hr-archive-table th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .hr-archive-table tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .hr-archive-table .action-buttons button {
            margin: 0 5px;
            padding: 5px 10px;
            cursor: pointer;
        }

        /* 模态框样式 */
        .modal {
            display: none; /* Hidden by default */
            position: fixed; /* Stay in place */
            z-index: 1; /* Sit on top */
            left: 0;
            top: 0;
            width: 100%; /* Full width */
            height: 100%; /* Full height */
            overflow: auto; /* Enable scroll if needed */
            background-color: rgb(0,0,0); /* Fallback color */
            background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
        }

        .modal-content {
            background-color: #fefefe;
            margin: 15% auto; /* 15% from the top and centered */
            padding: 20px;
            border: 1px solid #888;
            width: 80%; /* Could be more or less, depending on screen size */
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
        }

        .form-group input,
        .form-group select,
        .form-group textarea {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<div class="navbar">
    <div class="logo">
        <img src="/image/校徽.png" alt="图标">
    </div>
    <div class="system-name">人力资源管理系统</div>
    <div class="titles">
        <a href="index_jingli.jsp">首页</a>
        <a href="#about">关于</a>
        <a href="#contact">联系我们</a>
    </div>
</div>

<!-- 页面主体布局 -->
<div class="main-container">
    <!-- 左侧垂直菜单 -->
    <div class="sidebar">
        <div class="menu-item" data-tooltip="人力资源管理模块">
            人力资源管理模块
            <div class="submenu">
                <a href="humanAdd_jingli.jsp">添加档案</a>
                <a href="humanQuery">档案查询和管理</a>
            </div>
        </div>
        <div class="menu-item" data-tooltip="管理薪酬">
            薪酬管理模块
        </div>
    </div>

    <!-- 右侧内容框 -->
    <div class="content">
        <h1>人力资源档案数据</h1>
        <table class="hr-archive-table">
            <thead>
            <tr>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>电话</th>
                <th>所属部门</th>
                <th>薪酬标准</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="hr-archive-data">
            <!-- 数据将在这里动态插入 -->
            </tbody>
        </table>
    </div>
</div>

<script>
    $(document).ready(function() {
        // 获取人力资源数据并显示在表格中
        $.ajax({
            url: 'human', // 确保这个URL路径是正确的
            method: 'GET',
            success: function(response) {
                console.log('Response:', response); // 打印响应数据到控制台
                if (response.code === 1) {
                    var hrArchiveList = response.data;
                    var tableBody = $('#hr-archive-data');
                    tableBody.empty();

                    if (hrArchiveList && hrArchiveList.length > 0) {
                        hrArchiveList.forEach(function(hrArchive) {
                            var genderText = hrArchive.gender === 1 ? '男' : '女';
                            var row = `<tr data-hr-archive='${JSON.stringify(hrArchive)}'>
                                <td>${hrArchive.name || ''}</td>
                                <td>${genderText}</td>
                                <td>${hrArchive.age || ''}</td>
                                <td>${hrArchive.phone || ''}</td>
                                <td>${hrArchive.department_name || ''}</td>
                                <td>${hrArchive.salary || ''}</td>
                                <td class="action-buttons">
                                    <button class="edit-btn" data-id="${hrArchive.id}">修改</button>
                                    <button class="delete-btn" data-id="${hrArchive.id}">删除</button>
                                </td>
                            </tr>`;
                            console.log('Appending row:', row); // 新增调试信息
                            tableBody.append(row);
                        });
                    } else {
                        tableBody.append('<tr><td colspan="7">没有数据</td></tr>');
                    }
                } else {
                    alert('获取数据失败：' + response.msg);
                }
            },
            error: function(xhr, status, error) {
                console.error('请求失败：', xhr, status, error); // 打印错误信息到控制台
                alert('请求失败：' + error);
            }
        });

        // 绑定修改按钮点击事件
        $(document).on('click', '.edit-btn', function() {
            var row = $(this).closest('tr');
            var hrArchive = JSON.parse(row.attr('data-hr-archive'));
            showEditModal(hrArchive);
        });

        // 显示编辑模态框
        function showEditModal(hrArchive) {
            var modalContent = `
                <div id="edit-modal" class="modal">
                    <div class="modal-content">
                        <span class="close">&times;</span>
                        <h2>编辑人力资源档案</h2>
                        <form id="edit-form">
                            <input type="hidden" id="id" name="id" value="${hrArchive.id}">
                            <div class="form-group">
                                <label for="name">姓名:</label>
                                <input type="text" id="name" name="name" value="${hrArchive.name}">
                            </div>
                            <div class="form-group">
                                <label for="gender">性别:</label>
                                <select id="gender" name="gender">
                                    <option value="1" ${hrArchive.gender === 1 ? 'selected' : ''}>男</option>
                                    <option value="2" ${hrArchive.gender === 2 ? 'selected' : ''}>女</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="age">年龄:</label>
                                <input type="number" id="age" name="age" min="1" max="99" value="${hrArchive.age}">
                            </div>
                            <div class="form-group">
                                <label for="phone">电话:</label>
                                <input type="text" id="phone" name="phone" value="${hrArchive.phone || ''}">
                            </div>
                            <div class="form-group">
                                <label for="department_name">所属部门:</label>
                                <input type="text" id="department_name" name="department_name" value="${hrArchive.department_name || ''}">
                            </div>
                            <div class="form-group">
                                <label for="salary">薪酬标准:</label>
                                <input type="number" id="salary" name="salary" value="${hrArchive.salary}">
                            </div>
                            <div class="form-group">
                                <label for="bank_name">开户银行:</label>
                                <input type="text" id="bank_name" name="bank_name" value="${hrArchive.bank_name || ''}">
                            </div>
                            <div class="form-group">
                                <label for="bank_account">银行账号:</label>
                                <input type="text" id="bank_account" name="bank_account" value="${hrArchive.bank_account || ''}">
                            </div>
                            <div class="form-group">
                                <label for="id_card">身份证号码:</label>
                                <input type="text" id="id_card" name="id_card" value="${hrArchive.id_card || ''}" readonly>
                            </div>
                            <div class="form-group">
                                <label for="personal_description">个人介绍:</label>
                                <textarea id="personal_description" name="personal_description">${hrArchive.personal_description || ''}</textarea>
                            </div>
                            <div class="form-group">
                                <label for="remarks">备注:</label>
                                <textarea id="remarks" name="remarks">${hrArchive.remarks || ''}</textarea>
                            </div>
                            <div class="form-group">
                                <button type="button" id="cancel-btn">返回</button>
                                <button type="button" id="save-btn">确认修改</button>
                            </div>
                        </form>
                    </div>
                </div>
            `;

            $('body').append(modalContent);

            // 关闭模态框
            $('.close, #cancel-btn').click(function() {
                $('#edit-modal').remove();
            });

            // 显示模态框
            $('#edit-modal').css('display', 'block');

            // 确认修改按钮点击事件
            $('#save-btn').click(function() {
                var id = $('#id').val();
                var name = $('#name').val();
                var gender = $('#gender').val();
                var age = $('#age').val();
                var phone = $('#phone').val();
                var department_name = $('#department_name').val();
                var salary = $('#salary').val();
                var bank_name = $('#bank_name').val();
                var bank_account = $('#bank_account').val();
                var id_card = $('#id_card').val();
                var personal_description = $('#personal_description').val();
                var remarks = $('#remarks').val();

                // 构建要发送的数据对象
                var updatedHrArchive = {
                    id: parseInt(id),
                    name: name,
                    gender: parseInt(gender),
                    age: parseInt(age),
                    phone: phone,
                    department_name: department_name,
                    salary: parseInt(salary),
                    bank_name: bank_name,
                    bank_account: bank_account,
                    id_card: id_card,
                    personal_description: personal_description,
                    remarks: remarks
                };

                // 发送 AJAX 请求更新数据
                $.ajax({
                    url: 'human', // 后端接口路径
                    method: 'PUT',
                    contentType: 'application/json',
                    data: JSON.stringify(updatedHrArchive),
                    success: function(response) {
                        if (response.code === 1) {
                            alert('更新成功！');
                            // 刷新表格数据
                            location.reload();
                        } else {
                            alert('更新失败：' + response.msg);
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error('请求失败：', xhr, status, error);
                        alert('请求失败：' + error);
                    }
                });
            });
        }

        // 绑定删除按钮点击事件
        $(document).on('click', '.delete-btn', function() {
            var id = $(this).data('id');
            if (confirm('确定要删除该记录吗？')) {
                $.ajax({
                    url: `/human/delete/${id}`, // 后端删除接口路径
                    method: 'DELETE',
                    success: function(response) {
                        if (response.code === 1) {
                            alert('删除成功！');
                            // 刷新表格数据
                            location.reload();
                        } else {
                            alert('删除失败：' + response.msg);
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error('请求失败：', xhr, status, error);
                        alert('请求失败：' + error);
                    }
                });
            }
        });
    });

    // JavaScript 实现下拉菜单功能
    document.querySelectorAll('.menu-item').forEach(item => {
        item.addEventListener('click', () => {
            // 切换 active 类，控制子菜单显示/隐藏
            item.classList.toggle('active');
        });
    });
</script>
</body>
</html>